<?php

namespace tbollmeier\kakuromaster;

set_include_path(".." . PATH_SEPARATOR . get_include_path());

require_once 'vendor/autoload.php';
require_once 'vendor/tbollmeier/graphutil/autoloader.php';
require_once 'autoloader.php';

use tbollmeier\kakuromaster\core\Puzzle as Puzzle;

function solve($json)
{
    $puzzle = Puzzle::createFromJson($json);
    
    printPuzzle($puzzle);

    $puzzle->solve();

    echo "\nSolution:\n";

    printPuzzle($puzzle);    
}

function printPuzzle($puzzle)
{
    $w = $puzzle->getWidth();
    $h = $puzzle->getHeight();
    
    for ($r=0; $r<$h; $r++) {
        $row = "";
        for ($c=0; $c<$w; $c++) {
            $cell = $puzzle->getCell($r, $c);
            if ($cell !== null) {
                switch (count($cell->values)) {
                    case 0:
                        $row .= "E";
                        break;
                    case 1:
                        $row .= $cell->values[0];
                        break;
                    default:
                        $row .= "?";
                }
            } else {
                $row .= "X";
            }
        }
        print $row . "\n";
    }
    
}

// ==================================================================

if ($argc !== 2) {
    echo "SYNTAX: $argv[0] <problem_file.json>";
    exit(1);
}

$jsonFile = $argv[1];
$json = file_get_contents($jsonFile);

solve($json);


